define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'monitor/device/index',
                    add_url: 'monitor/device/device_add',                        
                    edit_url: 'monitor/device/device_edit',
                    del_url: 'monitor/device/device_del',
                    multi_url: 'monitor/device/multi',                    
                    table: '',                    
                }
            });

            var table = $("#table");
            var alarm_way_arr = {"1":__('邮箱'),"2":__('企业微信'),"3":__('钉钉'),"9":__('全部')};
            var switch_arr = {"1":__('开'),"0":__('关')};
            var mail_group = Config.group;
            var department_data = Config.department_data;
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,                
                sortName: 'id',
                search: false,
                showToggle: false,
                showColumns: false,
                // showExport:false,
                columns: [
                        [
                        {checkbox: true},
                        {field: 'ops_alarm_device.id', title: __('ID'),formatter: function(value,row,index){
                            return row.id;
                        }},
                        {field: 'ops_alarm_device.ip', title: __('设备IP'),operate:'LIKE',placeholder: __('模糊查询'),formatter: function(value,row,index){
                            return row.ip;
                        }},
                        {field: 'c.name', title: __('设备名'),operate:'LIKE',placeholder: __('模糊查询'),formatter: function(value,row,index){
                            return row.device_name;
                        }},
                        // {field: 'ops_alarm_device.template_id', title: __('Alarm Template'),visible:false,searchList:$.getJSON('monitor/Alarmtemplate/searchSource')},
                        // {field: 'template_id', title: __('Alarm Template'),operate:false},
                        {field: 'alarm_way', title: __('告警方式'),searchList:alarm_way_arr,formatter:function(value,row,index){
                            if(value == 1){
                                return '邮箱';
                            }else if(value == 2){
                                return '企业微信';
                            }else if(value == 3){
                                return '钉钉';
                            }else{
                                return '全部';
                            }                           
                        }},
                        // {field: 'IfDescr', title: __('设备接口'),operate:false,formatter:function(value,row,index){
                        //     // if(row.IfAlias == null || row.IfIndex == null){
                        //     //     return "-";
                        //     // }else{
                        //     //     // console.log(456);
                        //     //     return (row.IfAlias?row.IfAlias:row.IfDescr)+"__"+row.IfIndex;                      
                        //     // }
                        //     if(row.IfAlias == null && row.IfIndex == null){
                        //         return "-";
                        //     }else{
                        //         if(row.IfDescr != null){
                        //           var a = (row.IfDescr).split(',');
                        //         }else{
                        //           var a = new Array();
                        //         }
                        //         if(row.IfAlias != null){
                        //           var b = (row.IfAlias).split(',');
                        //         }else{
                        //           var b = new Array();
                        //         }
                        //         if(row.IfIndex != null){
                        //           var c = (row.IfIndex).split(',');
                        //         }else{
                        //           var c = new Array();
                        //         }
                        //         // var a = (row.IfDescr).split(',');
                        //         // var b = (row.IfAlias).split(',');                                
                        //         // var c = (row.IfIndex).split(',');
                        //         item = new Array();
                        //         if(a.length > 1){
                        //             a.forEach(function(v,k){
                        //                 if(c[k]){
                        //                     item[k] =  (b.k?b.k:v)+'__'+c[k];
                        //                 }else{
                        //                     item[k] =  (b.k?b.k:v);
                        //                 }
                        //                 // console.log(row.device_name+'__'+(b.k?b.k:v)+'__'+c[k]);
                        //             })
                        //             return item.join(',');
                        //         }else{
                        //             return (row.IfAlias?row.IfAlias:row.IfDescr)+"__"+row.IfIndex;
                        //         }
                        //     }                        
                        // }},                      
                        // // {field: 'IfIndex', title: __('接口编号'),operate:false},                   
                        // // {field: 'IfAlias', title: __('接口描述别名'),operate:false,formatter:function(value,row,index){
                        // //     if(!value){
                        // //         return '-';
                        // //     }else{
                        // //         return value;
                        // //     }                           
                        // // }},
                        // {field: 'f.id', title: __('企业微信部门'),searchList:department_data,operate:'LIKE',formatter:function(value,row,index){
                        //     return row.department_name;
                        // }},
                        // {field: 'g.name', title: __('企业微信成员'),operate:'LIKE',placeholder: __('模糊查询'),formatter:function(value,row,index){
                        //     return row.user_name;
                        // }},
                        // {field: 'e.id', title: __('邮件组'),searchList:mail_group,operate:'LIKE %...%',formatter:function(value,row,index){
                        //     return row.group_name;
                        // }},
                        // {field: 'd.number', title: __('邮件接收人'),operate:'LIKE',placeholder: __('模糊查询'),formatter:function(value,row,index){
                        //     return row.mail;
                        // }},
                        {
                            field: 'switch',
                            title: __('开关'),
                            align: 'center',
                            searchList:switch_arr,
                            formatter: Table.api.formatter.toggle
                        },
                        // {field: 'IfType', title: __('接口类型'),operate:false},  
                        {field: 'ops_alarm_device.createtime', title: __('Createtime'),operate:'RANGE',addclass:'datetimerange',
                            formatter: function(value,row,index){
                                var date = new Date((row.createtime)*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                                Y = date.getFullYear() + '-';
                                M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                                D = (date.getDate() < 10 ? '0'+(date.getDate()):date.getDate()) + ' ';
                                h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours()) + ':';
                                m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
                                s = date.getSeconds() < 10 ? '0' + date.getSeconds() :date.getSeconds();
                                return Y+M+D+h+m+s;
                            }
                        },
                        {field: 'ops_alarm_device.updatetime', title: __('更新时间'), operate:'RANGE', addclass:'datetimerange',
                            formatter: function(value,row,index){
                                var date = new Date((row.updatetime)*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                                Y = date.getFullYear() + '-';
                                M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
                                D = (date.getDate() < 10 ? '0'+date.getDate():date.getDate()) + ' ';
                                h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours()) + ':';
                                m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
                                s = date.getSeconds() < 10 ? '0' + date.getSeconds() :date.getSeconds();
                                return Y+M+D+h+m+s;
                            }
                        },                   
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'monitor/device/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                        ]
                    ],
                // commonSearch: false,
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {            
            Controller.api.bindevent();
        },
        edit: function () {            
            Controller.api.bindevent();
        },
        get: function () {
            Controller.api.bindevent();
        },
        // 时间转换方法
        format: function(time, format) {
            var t = new Date(time);
            var tf = function(i) {
              return (i < 10 ? '0' : '') + i
            };
            return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
              switch (a) {
                case 'yyyy':
                  return tf(t.getFullYear());
                  break;
                case 'MM':
                  return tf(t.getMonth() + 1);
                  break;
                case 'mm':
                  return tf(t.getMinutes());
                  break;
                case 'dd':
                  return tf(t.getDate());
                  break;
                case 'HH':
                  return tf(t.getHours());
                  break;
                case 'ss':
                  return tf(t.getSeconds());
                  break;
              }
            })
        },
        device_add:function(){
            $("#c-mail_group_ids").attr("readOnly","true");            
            $("#c-mail_ids").attr("readOnly","true");
            $("#c-wechat_department_ids").attr("readOnly","true");
            $("#c-wechat_user_ids").attr("readOnly","true");             
            //  选择不同的报警方式展示不同的列表
            var alarm_way_data = $("select[name='row[alarm_way]']").find('option:selected').val();
            $("select[name='row[alarm_way]']").change(function(){                
               var data_name = $("select[name='row[alarm_way]']").find('option:selected').val();
               if(data_name == '9'){
                    $("#mail").show();
                    $("#weach").show();            
                    $("#ding").show();
               }else if(data_name == '1'){
                    $("#mail").show();
                    $("#weach").hide();
                   $("#ding").hide();

               }else if(data_name == '2'){
                    $("#mail").hide();
                    $("#weach").show();
                   $("#ding").hide();

               }else if(data_name == '3'){
                    $("#mail").hide();
                    $("#weach").hide();
                   $("#ding").show();

               }
            })
            //设备IP切换的时候  设备接口的值清空
            $("select[name='row[ip]']").change(function(){        
                $("#c-device_if_ids").val("");
                $(".selected_tag").remove();        
            })            
            $("#c-device_if_ids").data("params",function(obj){
                return {row:{device_id:$("#ip").val()}};
            });
            Controller.api.bindevent();
        },
        device_edit:function(){
            $("#c-mail_group_ids").attr("readOnly","true");            
            $("#c-mail_ids").attr("readOnly","true");
            $("#c-wechat_department_ids").attr("readOnly","true");
            $("#c-wechat_user_ids").attr("readOnly","true");   
            var alarm_way_data = $("select[name='row[alarm_way]']").find('option:selected').val();
            if(alarm_way_data == 9){
                $("#mail").show();
                $("#weach").show();
                $("#ding").show();
            }else if(alarm_way_data == 1){
                $("#mail").show();
                $("#weach").hide();
                $("#ding").hide();
            }else if(alarm_way_data == 2){
                $("#mail").hide();
                $("#weach").show();
                $("#ding").hide();
            }else if(alarm_way_data == 3){
                $("#mail").hide();
                $("#weach").hide();
                $("#ding").show();
            }
            //  选择不同的报警方式展示不同的列表
            $("select[name='row[alarm_way]']").change(function(){
               var data_name = $("select[name='row[alarm_way]']").find('option:selected').val();
               if(data_name == '9'){
                    $("#mail").show();
                    $("#weach").show();
                    $("#ding").show();
               }else if(data_name == '1'){
                    $("#mail").show();
                    $("#weach").hide();
                    $("#ding").hide();
               }else if(data_name == '2'){
                    $("#mail").hide();
                    $("#weach").show();
                    $("#ding").hide_icons();
               }else if(data_name == '3'){
                    $("#mail").hide();
                    $("#weach").hide();
                    $("#ding").show();
               }
            })
            //设备IP切换的时候  设备接口的值清空
            $("select[name='row[ip]']").change(function(){        
                $("#c-device_if_ids").val("");        
                $('#device').find(".selected_tag").remove();
            })
            $("#c-device_if_ids").data("params",function(obj){
                return {row:{device_id:$("#ip").val()}};
            });         
            Controller.api.bindevent();
        },  
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});